package com.example.test1.moudle;

import android.widget.Switch;

import com.example.test1.http.Api;
import com.example.test1.http.MySerer;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by 段傅华 on 2018/12/12.
 */

public class Moudle implements Main_Moudle{
    @Override
    public void setdata(String url, RequestBody requestBody, final Api api, final Finish finish) {
        Retrofit build = new Retrofit.Builder()
                .baseUrl(MySerer.mUrl)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
        MySerer mySerer = build.create(MySerer.class);
        switch (api){
            case CHANNEL:
                Observable<String> stringObservable = mySerer.postChannel(url, requestBody);
                stringObservable.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(String value) {
                            finish.setSuccess(value,api);
                            }

                            @Override
                            public void onError(Throwable e) {
                            finish.setError(e.getMessage());
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
                break;
            case LIST:
                Observable<String> stringObservable11 = mySerer.postList(url, requestBody);
                stringObservable11.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(String value) {
                            finish.setSuccess(value,api);
                            }

                            @Override
                            public void onError(Throwable e) {
                                finish.setError(e.getMessage());
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
                break;
        }
    }
}
